//
//  GYCompayListView.m
//  LockManagement
//
//  Created by 丁燕春 on 2018/8/3.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYCompayListView.h"
#import "GYCompany.h"

@interface GYCompayListView()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *companyListTablevIew;

/** 公司清单 */
@property (nonatomic, strong) NSArray *companyList;
@end

@implementation GYCompayListView

+ (instancetype)shareView {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.4];
    self.layer.cornerRadius = 5;
    
    //加载数据
    [GYCompany getComapngList:^(NSArray *companylists) {
        self.companyList = companylists;
        [self.companyListTablevIew reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint point = [touch locationInView:[touch view]]; //返回触摸点在视图中的当前坐标
    if (!CGRectContainsPoint(self.companyListTablevIew.frame, point)) {
        [self removeFromSuperview];
    }
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.companyList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *companyCellIdentifier = @"companyCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:companyCellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:companyCellIdentifier];
    }
    GYCompany *model = self.companyList[indexPath.row];
    cell.textLabel.text = model.companyName;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.companySelectCallBack(self.companyList[indexPath.row]);
}
@end
